var area = require('../../utils/area.js')

var areaInfo = []; //所有省市区县数据

var provinces = []; //省

var citys = []; //城市

var countys = []; //区县

var index = [0, 0, 0];

var cellId;

var t = 0;
var show = false;
var moveY = 200;

Page({
  data: {
    show: show,
    provinces: provinces,
    citys: citys,
    countys: countys,
    value: [0, 0, 0],
    name: "",
    phone: "",
    address: "",
    area:"",
    addrId: "",
    singleAddress: "",
    lastAddress: "",
    go:1,
    region:""
  },
  //滑动事件
  bindChange: function(e) {
    var val = e.detail.value
    // console.log(e)
    //判断滑动的是第几个column
    //若省份column做了滑动则定位到地级市和区县第一位
    if (index[0] != val[0]) {
      val[1] = 0;
      val[2] = 0;
      getCityArr(val[0], this); //获取地级市数据
      getCountyInfo(val[0], val[1], this); //获取区县数据
    } else { //若省份column未做滑动，地级市做了滑动则定位区县第一位
      if (index[1] != val[1]) {
        val[2] = 0;
        getCountyInfo(val[0], val[1], this); //获取区县数据
      }
    }
    index = val;

    console.log(index + " => " + val);

    //更新数据
    this.setData({
      value: [val[0], val[1], val[2]],
      province: provinces[val[0]].name,
      city: citys[val[1]].name,
      county: countys[val[2]].name
    })

  },
  onLoad: function(options) {
    console.log(options);
    var that = this;
    this.setData({
      addrId: options.addrId,
      go:options.go
    })
    if (that.data.addrId == undefined) {

    } else {
      wx.request({
        url: 'https://www.xiaozheng8.com/addr/findAll',
        data: {
          userId: wx.getStorageSync("userId"),
        },
        method: 'post',
        success: function(res) {
          console.log(res)
          for (var i = 0; i < res.data.data.length; i++) {
            if (res.data.data[i].addrId == that.data.addrId) {
              address = res.data.data[i]
            }
          }
          console.log(address);
          that.setData({
              name: address.name,
              phone: address.phone,
              singleAddress: address.addr,
              area: address.area
            }),
            console.log(that.data.singleAddress)
        },
        fail: function(res) {},
        complete: function(res) {},
      })
    }
    // wx.request({
    //   url: 'https://www.xiaozheng8.com/addr/update',
    //   data: {
    //     addrId: that.data.addrId
    //   },
    //   method: 'post',
    //   success: function(res) {
    //     console.log(res)
    //   },
    //   fail: function(res) {},
    //   complete: function(res) {},
    // })
    cellId = options.cellId;
    var that = this;
    var date = new Date()
    console.log(date.getFullYear() + "年" + (date.getMonth() + 1) + "月" + date.getDate() + "日");

    //获取省市区县数据
    area.getAreaInfo(function(arr) {
      areaInfo = arr;
      //获取省份数据
      getProvinceData(that);
    });

  },
  name: function(e) {
    this.setData({
      name: e.detail.value
    })
  },
  phone: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  address: function(e) {
    this.setData({
      address: e.detail.value
    })
  },

  // ------------------- 分割线 --------------------
  onReady: function() {
    this.animation = wx.createAnimation({
      transformOrigin: "50% 50%",
      duration: 0,
      timingFunction: "ease",
      delay: 0
    })
    this.animation.translateY(200 + 'vh').step();
    this.setData({
      animation: this.animation.export(),
      show: show
    })
  },
  //移动按钮点击事件
  translate: function(e) {
    if (t == 0) {
      moveY = 0;
      show = false;
      t = 1;
    } else {
      moveY = 200;
      show = true;
      t = 0;
    }
    // this.animation.translate(arr[0], arr[1]).step();
    animationEvents(this, moveY, show);

  },
  // 微信小程序自带地址三级选择
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  //隐藏弹窗浮层
  // hiddenFloatView(e) {
  //   console.log(e);
  //   moveY = 200;
  //   show = true;
  //   t = 0;
  //   animationEvents(this, moveY, show);

  // },
  //页面滑至底部事件
  onReachBottom: function() {
    // Do something when page reach bottom.
  },
  save: function() {
    var that = this;
    console.log(that.data.province)
    if (that.data.name == "") {
      wx.showToast({
        title: '请输入用户名',
        icon: "none"
      })
    } else if (that.data.phone == "") {
      wx.showToast({
        title: '请输入电话号码',
        icon: "none"
      })
    } else if (!(/^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/.test(that.data.phone))) {
      wx.showToast({
        title: '电话号码错误请重新输入',
        icon: "none"
      })
    } else if (that.data.region == "") {
      wx.showToast({
        title: '请填写所在地区',
        icon: "none"
      })

    } else if (that.data.address == ""&&that.data.area=="") {
      wx.showToast({
        title: '请输入详细地址',
        icon: "none"
      })
    } else {
      if (that.data.addrId == undefined) {
        wx.request({
          url: 'https://www.xiaozheng8.com/addr/add',
          data: {
            name: that.data.name,
            phone: that.data.phone,
            addr: that.data.region[0] + " " + that.data.region[1] + " " + that.data.region[2],
            area: that.data.address,
            userId: wx.getStorageSync("userId")
          },
          method: 'post',
          success: function(res) {
            console.log(res)
          },
          fail: function(res) {},
          complete: function(res) {},
        })
      } else {
        wx.request({
          url: 'https://www.xiaozheng8.com/addr/update',
          data: {
            addrId: that.data.addrId,
            userId: wx.getStorageSync("userId")
          },
          success: function(res) {
            console.log(res);
           
          },
          fail: function(res) {

          }
        })
       
      }
      console.log(that.data.go);
      wx.redirectTo({
        url: '../address/address?go=' + that.data.go,
      })

     
    }
  }
})

//动画事件
function animationEvents(that, moveY, show) {
  console.log("moveY:" + moveY + "\nshow:" + show);
  that.animation = wx.createAnimation({
    transformOrigin: "50% 50%",
    duration: 400,
    timingFunction: "ease",
    delay: 0
  })
  that.animation.translateY(moveY + 'vh').step()

  that.setData({
    animation: that.animation.export(),
    show: show
  })

}

// ---------------- 分割线 ---------------- 

//获取省份数据
function getProvinceData(that) {
  var s;
  provinces = [];
  var num = 0;
  for (var i = 0; i < areaInfo.length; i++) {
    s = areaInfo[i];
    if (s.di == "00" && s.xian == "00") {
      provinces[num] = s;
      num++;
    }
  }
  that.setData({
    provinces: provinces
  })

  //初始化调一次
  getCityArr(0, that);
  getCountyInfo(0, 0, that);
  that.setData({
    province: "",
    city: "",
    county: "",
  })

}

// 获取地级市数据
function getCityArr(count, that) {
  var c;
  citys = [];
  var num = 0;
  for (var i = 0; i < areaInfo.length; i++) {
    c = areaInfo[i];
    if (c.xian == "00" && c.sheng == provinces[count].sheng && c.di != "00") {
      citys[num] = c;
      num++;
    }
  }
  if (citys.length == 0) {
    citys[0] = {
      name: ''
    };
  }

  that.setData({
    city: "",
    citys: citys,
    value: [count, 0, 0]
  })
}

// 获取区县数据
function getCountyInfo(column0, column1, that) {
  var c;
  countys = [];
  var num = 0;
  for (var i = 0; i < areaInfo.length; i++) {
    c = areaInfo[i];
    if (c.xian != "00" && c.sheng == provinces[column0].sheng && c.di == citys[column1].di) {
      countys[num] = c;
      num++;
    }
  }
  if (countys.length == 0) {
    countys[0] = {
      name: ''
    };
  }
  that.setData({
    county: "",
    countys: countys,
    value: [column0, column1, 0]
  })
}